//
//  STApplicationFormViewController.m
//  赛拓
//
//  Created by 王磊 on 8/13/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STApplicationFormViewController.h"
#import "STApplicationFormTableView.h"
#import "STApplicationFormBean.h"
#import "STPerfectInfoViewController.h"
#import "STWaitCheckingViewController.h"
@interface STApplicationFormViewController ()<STBaseViewDelegate ,UINavigationControllerDelegate ,UIImagePickerControllerDelegate>
@property (nonatomic ,strong) STApplicationFormTableView *tableView;
// 三证合一
@property (nonatomic ,strong) NSArray *credentialsUnite;
// 三证不合一
@property (nonatomic ,strong) NSArray *noneCredentialsUnite;
// 是否三证合一
@property (nonatomic ,assign) BOOL isCredentialsUnite;
@end

@implementation STApplicationFormViewController

- (NSArray *)credentialsUnite {
    if (!_credentialsUnite) {
        NSArray *data = @[
                          @{
                              @"title":@"营业执照",
                              @"icon":@""
                              },
                          @{
                              @"title":@"开户许可证",
                              @"icon":@""
                              },
                          @{
                              @"title":@"法人身份证",
                              @"icon":@""
                              },
                          @{
                              @"title":@"公司标志",
                              @"icon":@""
                              }
                          ,
                          @{
                              @"title":@"威化经营许可证",
                              @"icon":@""
                              }
                          ];
        _credentialsUnite = [STApplicationFormBean mj_objectArrayWithKeyValuesArray:data];
    }
    return _credentialsUnite;
}
- (NSArray *)noneCredentialsUnite {
    if (!_noneCredentialsUnite) {
        
        NSArray *data = @[
                          @{
                              @"title":@"营业执照",
                              @"icon":@""
                              },
                          @{
                              @"title":@"组织机构代码",
                              @"icon":@""
                              }
                          
                          ,
                          @{
                              @"title":@"税务登记证",
                              @"icon":@""
                              }
                          
                          ,@{
                              @"title":@"开户许可证",
                              @"icon":@""
                              },
                          @{
                              @"title":@"法人身份证",
                              @"icon":@""
                              },
                          @{
                              @"title":@"公司标志",
                              @"icon":@""
                              }
                          
                          ,
                          @{
                              @"title":@"威化经营许可证",
                              @"icon":@""
                              }
                          ];
        _noneCredentialsUnite = [STApplicationFormBean mj_objectArrayWithKeyValuesArray:data];
    }
    return _noneCredentialsUnite;
}
- (void)initSubviews {
    [super initSubviews];
    
    [self initTableView];
    
}
- (void)initTableView {
    
    STApplicationFormTableView *tableView = [STApplicationFormTableView instanceWithFrame:CGRectMake(0, 0, ST_SCREEN_WIDTH, CGRectGetHeight(self.view.bounds) - 40)];
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    
    tableView.mDelegate = self;
    
    tableView.cellType = STCellTypeApplicationForm;
}
- (void)loadData {
    
    [self.tableView updateData:self.noneCredentialsUnite];
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    @WeakObj(self)
    switch (itemType) {
        case STItemClickTypeItem:
        {
            
            
        }
            break;
            // 证件上传
        case STItemClickTypeCredentialsUpload:
        {
            
            @WeakObj(self)
            ACActionSheet *sheet = [[ACActionSheet alloc]initWithTitle:@"选取证件照片" cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonRedTitles:@[@"拍照",@"从相册中选取"] actionSheetBlock:^(NSInteger buttonIndex) {
                
                [selfWeak setupImagePick: buttonIndex];
                
            }];
            [sheet show];
        }
            break;
            //修改个人资料
        case STItemClickTypeItemMaterial: {
            
            STPerfectInfoViewController *perfectInfo = [STPerfectInfoViewController instance];
            
            //            perfectInfo.indexPath = indexPath;
            
            perfectInfo.block = ^(id blockItemClickType,id blockData,id blockIndexPath) {
                STApplicationFormBean *formBean =data;
                
                formBean.infoDes = blockData;
                
                [selfWeak.tableView updateHeader:formBean forIndexPath:indexPath];
            };
            
            [self.navigationController pushViewController:perfectInfo animated:true];
        }
            break;
            // 三证合一
        case STItemClickTypeSwitch: {
            
            BOOL selected = [data boolValue];
            
            if (selected) {
                
                [self.tableView updateData:self.credentialsUnite];
                
            } else {
                
                [self.tableView updateData:self.noneCredentialsUnite];
            }
        }
            break;
        case STItemClickTypeComplete:
        {
            STWaitCheckingViewController *waitCheck = [STWaitCheckingViewController instance];
            
            [self.navigationController pushViewController:waitCheck animated:true];
        }
            break;
        default:
            break;
    }
    
    
}
#pragma mark ---- take photos

- (void)setupImagePick:(NSInteger )index {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    imagePickerController.allowsEditing = true;
    
    imagePickerController.delegate = self;
    
    if (index == 0) {
        
        if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
            imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
            
        } else {
            
            NSLog(@"模拟器 不能 使用相机");
        }
    }else if (index == 1){
        
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
    }
    [self presentViewController:imagePickerController animated:true completion:nil];
}

#pragma mark ----- UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    // 选取相册中的图片
    NSString *mediaType = info[UIImagePickerControllerMediaType];
    
    UIImage *image = nil;
    
    UIImage *uploadImage = nil;
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        
        image = info[UIImagePickerControllerEditedImage];
        // 压缩图片
        NSData *data = UIImageJPEGRepresentation(image, 0.1);
        
        uploadImage = [UIImage imageWithData:data];
        
    }
    @WeakObj(self)
    [picker dismissViewControllerAnimated:true completion:^{
        __strong typeof(selfWeak) strongSelf = selfWeak;
        [strongSelf upload:uploadImage];
    }];
    
}
- (void)upload:(UIImage *)image {
    
    
}
@end
